<?php
namespace App\Http\Controllers\Admin;

use App\Service\Admin\BrandService;
use App\Service\Admin\ImageService;
use App\Utilitys\Upload;
use Illuminate\Http\Request;

class BrandController extends BaseController
{
    //品牌管理

    //列表
    public function getIndex(Request $request)
    {
        $pages['start'] = $request->post('start', 1);
        $pages['limit'] = $request->post('limit', 10);
        $res = (new BrandService())->getList($pages);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data'], $res['total']);
    }

    //添加
    public function add(Request $request)
    {
        $this->validate($request, [
            'cover_type' => 'required',
            'cover' => 'required',
            //'gno' => 'required',
            //'detail' => 'required',
        ], [
            'cover_type.required' => '封面必须的',
            'cover.required' => '封面必须的',
            //'gno.required' => '商品编号必须的',
            //'detail.required' => '详情必须的',
        ]);
        $id = $request->post('id', 0);
        $pas['type'] = $request->post('type', 0);
        $pas['cover_type'] = $request->post('cover_type', 0);
        $pas['cover'] = $request->post('cover', '');
        $pas['gno'] = $request->post('gno', '');
        $pas['detail'] = $request->post('detail', '');
        $pas['vlink'] = $request->post('vlink', '');
        if (!$id) {
            $res = (new BrandService())->add($pas);
        } else {
            $res = (new BrandService())->modify($id, $pas);
        }
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson();
    }

    //详情
    public function getOne(Request $request)
    {
        $this->validate($request, [
            'id' => 'required',
        ], [
            'id.required' => 'ID必须的',
        ]);
        $id = $request->post('id', 0);
        $res = (new BrandService())->getOne($id);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data']);
    }

    //删除
    public function setDel(Request $request)
    {
        $this->validate($request, [
            'id' => 'required',
        ], [
            'id.required' => 'ID必须的',
        ]);
        $id = $request->post('id', 0);
        $res = (new BrandService())->setDel($id);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson();
    }

    //开关
    public function kgModify(Request $request)
    {
        $id = $request->post('id', 0);
        $kg = $request->post('kg', 0);
        $res = (new BrandService())->kgModify($id, $kg);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson();
    }

    //类型排序设置
    public function typeSort(Request $request)
    {
        $id = $request->post('id', 0);
        $oper = $request->post('oper', 0);
        $res = (new BrandService())->typeSort($id, $oper);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson();
    }

    //排序设置
    public function sort(Request $request)
    {
        $id = $request->post('id', 0);
        $oper = $request->post('oper', 0);
        $res = (new BrandService())->sort($id, $oper);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson();
    }

    //排序设置
    public function sortModify(Request $request)
    {
        $id = $request->post('id', 0);
        $sort = $request->post('sort', 0);
        $res = (new BrandService())->sortModify($id, $sort);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson();
    }
}